package com.example.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.test.common.P;
import com.example.test.common.SaveContext;
import com.example.test.domain.Book;
import com.example.test.domain.Borrow;
import com.example.test.domain.User;
import com.example.test.service.Bookservice;
import com.example.test.service.Borrowservice;
import com.example.test.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.ref.WeakReference;
import java.util.List;

@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    private Borrowservice borrowservice;
    @Autowired
    private Bookservice bookservice;
    @Autowired
    private Userservice userservice;
    @GetMapping("/page")
    public P<List<Borrow>> page(int page, String name){
//        System.out.println(page+name);
        //分页构造器
        Page<Borrow> pageInfo=new Page(page,10);
        //name可能传，可能不传
        LambdaQueryWrapper<Borrow> wrapper=new LambdaQueryWrapper();
        //过滤
        wrapper.like(name!=null,Borrow::getBookname,name);
        wrapper.eq(Borrow::getUserid,SaveContext.getCurrentId());
        //查询
        List<Borrow> list = borrowservice.page(pageInfo, wrapper).getRecords();
        return P.success(list);
    }
    @GetMapping("/borrow")
    public P<String> borrowbook(int id){
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserid,SaveContext.getCurrentId());
        User user = userservice.getOne(wrapper);
        LambdaQueryWrapper<Book> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(Book::getBookid,id);
        Book book = bookservice.getOne(wrapper1);
        LambdaQueryWrapper<Borrow> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(Borrow::getBookname,book.getBookname()).eq(Borrow::getUserid,user.getUserid());
        Borrow borrow = borrowservice.getOne(wrapper2);
        if(borrow!=null){//已经借过此书
            borrow.setBorrownumber(borrow.getBorrownumber()+1);
            book.setHaiyou(book.getHaiyou()-1);
            borrowservice.updateById(borrow);
            bookservice.updateById(book);
        }
        else{
            borrow=new Borrow();
            borrow.setBorrownumber(1);
            borrow.setBookname(book.getBookname());
            borrow.setUserid(user.getUserid());
            borrow.setUsername(user.getUsername());
            book.setHaiyou(book.getHaiyou()-1);
            borrowservice.save(borrow);
            bookservice.updateById(book);
        }
        return P.success("借到了");
    }
    @GetMapping("/huanshu")
    public P<String> huanshu(HttpServletRequest request){
        String borrowid = request.getParameter("borrowid");
        int num = Integer.parseInt(request.getParameter("num"));
        LambdaQueryWrapper<Borrow> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Borrow::getBorrowid,borrowid);
        Borrow borrow = borrowservice.getOne(wrapper);

        LambdaQueryWrapper<Book> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(Book::getBookname,borrow.getBookname());
        Book book = bookservice.getOne(wrapper1);
        book.setHaiyou(book.getHaiyou()+num);
        bookservice.update(book,wrapper1);

        borrow.setBorrownumber(borrow.getBorrownumber()-num);
        borrowservice.update(borrow,wrapper);
        if (borrow.getBorrownumber()==0){
            borrowservice.removeById(borrow.getBorrowid());
        }
        return P.success("还书成功");
    }
}
